/*********************************************************************************
 *      Copyright:  (C) 2022 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  packet.c
 *    Description:  This file is packet API functions
 *
 *        Version:  1.0.0(18/04/22)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "18/04/22 16:30:25"
 *
 ********************************************************************************/
 
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "packet.h"
#include "logger.h"
#include "ds18b20.h"
 
int get_devid(char *devid, int size, int sn)
{
    if( !devid || size<DEVID_LEN )
    {
        log_error("Invalid input arugments\n");
        return -1;
    }
 
    memset(devid, 0, size);
    snprintf(devid, size, "rpi#%04d", sn);
    return 0;
}
 
int get_time(struct tm *ptm)
{
    if( !ptm )
    {
        log_error("Invalid input arugments\n");
        return -1;
    }
 
    time_t now = time(NULL);
    localtime_r(&now, ptm);
 
    return 0;
}
 
int packet_segmented_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size)
{
    char              strtime[TIME_LEN] = {'\0'};
    struct tm        *ptm;
    char             *buf = (char *)pack_buf;
 
    if( !pack_info || !buf || size<=0 )
    {
        log_error("Invalid input arguments\n");
        return -1;
    }
 
    ptm = &pack_info->sample_time;
    snprintf(strtime, sizeof(strtime), "%04d-%02d-%02d %02d:%02d:%02d",
            ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,
            ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
 
    memset(buf, 0, size);
    snprintf(buf, size, "%s|%s|%.3f", pack_info->devid, strtime, pack_info->temper);
 
    return strlen(buf);
}
 
int packet_json_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size)
{
    char              strtime[TIME_LEN] = {'\0'};
    struct tm        *ptm;
    char             *buf = (char *)pack_buf;
 
    if( !pack_info || !buf || size<=0 )
    {
        log_error("Invalid input arguments\n");
        return -1;
    }
 
    ptm = &pack_info->sample_time;
    snprintf(strtime, sizeof(strtime), "%04d-%02d-%2d %02d:%02d:%02d",
            ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,
            ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
 
    memset(buf, 0, size);
    snprintf(buf, size, "{\"devid\":\"%s\", \"time\":\"%s\",\"temperature\":\"%.3f\"}",
            pack_info->devid, strtime, pack_info->temper);
 
    return strlen(buf);
}
 
/*
 * The CRC-ITU-T, also known as CRC-16-ITU-T or CRC-16-V.41, uses the polynomial:
 *     0x1021:  x^16 + x^12 + x^5 + 1
 */
 
#define CRC16_ITU_T_POLY 0x1021   /* Define the CRC-ITU-T polynomial */
static uint16_t crc_itu_t(const uint8_t *data, size_t length)
{
    uint16_t          crc = 0xFFFF;
    size_t            i, j;
 
    for (i=0; i<length; i++)
    {
        crc ^= ((uint16_t)data[i] << 8);
 
        for(j=0; j<8; j++)
        {
            if (crc & 0x8000)
            {
                crc = (crc << 1) ^ CRC16_ITU_T_POLY;
            }
            else
            {
                crc <<= 1;
            }
        }
    }
 
    return crc;
}
 
uint16_t to_big_endian(uint16_t num)
{
    return (num << 8) | (num >> 8);
}
 
/* TLV(Tag Length Value) PDU(Protocal Data Unit) format:
 *
 * +-----------+-----------+------------+-------------+-------------+
 * | Header(2B)|  Tag(1B)  | Length(2B) |  Value(nB)  |  CRC16(2B)  |
 * +-----------+-----------+------------+-------------+-------------+
 *
 * Header(2B): 0xFE 0xFD
 *    Tag(1B): 0x01->temperature 0x02->Humidity 0x03->Noisy ...
 * Length(2B): Data value length
 *  Value(nB): Data value
 *  CRC16(2B): CRC from Header to Value
 */
 
int packet_tlv_pack(pack_info_t *pack_info, uint8_t *pack_buf, int size)
{
    struct tm          *ptm;
    int                 ofset = 0;
    uint16_t            crc;
 
    if( !pack_info || !pack_buf || size<TLV_MINSIZE )
 
    {
        log_error("Invalid input arguments\n");
        return -1;
    }
 
    /*+----------------------+
     *|    TLV Header(2B)    |
     *+----------------------+*/
    *(uint16_t *)pack_buf = to_big_endian(TLV_HEADER);
    ofset += 2;
 
    /*+----------------------+
     *|      TLV Tag(1B)     |
     *+----------------------+*/
    pack_buf[ofset++] = TAG_TEMPERATURE;
 
    /* Skip data length here, we will calculate it later */
    ofset += 2;
 
    /*+----------------------+
     *|  payload data value  |
     *+----------------------+*/
 
    /* 6 bytes sample time */
    ptm = &pack_info->sample_time;
    pack_buf[ofset++] = (uint8_t)(ptm->tm_year+1900-2000);
    pack_buf[ofset++] = (uint8_t)(ptm->tm_mon+1);
    pack_buf[ofset++] = (uint8_t)(ptm->tm_mday);
    pack_buf[ofset++] = (uint8_t)(ptm->tm_hour);
    pack_buf[ofset++] = (uint8_t)(ptm->tm_min);
    pack_buf[ofset++] = (uint8_t)(ptm->tm_sec);
 
    /* 8 bytes device SN */
    strncpy((char *)(pack_buf+ofset), pack_info->devid, DEVID_LEN);
    ofset += DEVID_LEN;
 
    /* 2 bytes temperature value */
    pack_buf[ofset++] = (int)pack_info->temper;  /* integer part */
    pack_buf[ofset++] = (((int)(pack_info->temper*100))%100); /* fractional part */
 
    /*+----------------------+
     *|    TLV Length(2B)    |
     *+----------------------+*/
    *(uint16_t *)(pack_buf+3) = (ofset-5);
 
    /*+----------------------+
     *|      TLV CRC(2B)     |
     *+----------------------+*/
    crc = crc_itu_t(pack_buf, ofset);
    *(uint16_t *)(pack_buf+ofset) = crc;
    ofset += 2;
 
    return ofset;
}

